package com.wzu.rjgc_ks.controller;

import com.wzu.rjgc_ks.dao.NoticeMapper;
import com.wzu.rjgc_ks.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeAdminAction {

    @Autowired
    NoticeMapper noticeMapper;

    @RequestMapping("/add")
    public String addNotice(ModelMap modelMap){
        return "admin/notice/notice_add";
    }

    @RequestMapping("/add/save")
    public String saveNotice(Notice notice, ModelMap modelMap){

        notice.setNotTime(new Date());
        //实时session
        notice.setNotManaId(2);
        noticeMapper.insert(notice);
        return "success";
    }

    @RequestMapping("/show")
    public String showNotice(ModelMap modelMap){
        List<Notice> list= noticeMapper.selectAll();
        modelMap.addAttribute("notlist", list);

        return "admin/notice/notice_show";
    }

    @RequestMapping("/del")
    public String delNotice(@RequestParam(value = "NotId") Integer NotId, ModelMap modelMap){
        noticeMapper.deleteByPrimaryKey(NotId);
        return "success";
    }

    @RequestMapping("/fin")
    public String finNotice(ModelMap modelMap){
        return "success";
    }

    @RequestMapping("/edit/{id}")
    public String editNotice(@PathVariable("id") Integer id, ModelMap modelMap){
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        modelMap.addAttribute("notice", notice);
        return "admin/notice/notice_edit";
    }

    @RequestMapping("/edit/save/{id}")
    public String editSaveNotice(@PathVariable("id") Integer id, Notice notice, ModelMap modelMap){
        notice.setNotId(id);
        noticeMapper.updateByPrimaryKey(notice);
        return "success";
    }

}
